<?php

/**
 * The base controller which is used by the Front and the Admin controllers
 */
class MY_Model extends CI_Model {

	public $mytable = "";
	public $primaryKey = "";
	public $resCode = "";

	function __construct() {
		parent::__construct();
	}

	function field() {
		$fieldData = array();
		return $fieldData;
	}

	function get($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '')
			$this->db->where($param, $val);

		$this->db->from($this->mytable);
		return $this->db->get()->first_row();
	}

	function gets($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;
		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '')
			$this->db->where($param, $val);

		$this->db->from($this->mytable);
		return $this->db->get()->result();
	}

	function pagination($val = '', $param = null, $config = array()) {
		if ($param == null)
			$param = $this->primaryKey;
		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '')
			$this->db->where($param, $val);
		if (isset($config['cur_page']) && isset($config['per_page']))
			$this->db->limit($config['per_page'], ($config['cur_page'] - 1) * $config['per_page']);
		$this->db->from($this->mytable);
		return $this->db->get()->result();
	}

	function where_in($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		$this->db->from($this->mytable);
		if ($val != '')
			$this->db->where_in($this->mytable . "." . $param, $val);
		return $this->db->get()->result();
	}

	function num($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		$this->db->from($this->mytable);
		if ($val != '')
			$this->db->where($param, $val);
		return $this->db->count_all_results();
	}

	function update($val = '', $param = null, $arr_update) {
		if ($param == null)
			$param = $this->primaryKey;

		if ($val != '')
			$this->db->where($param, $val);
		$arr_update = $this->msystem->update_security($arr_update, $this->field());
		$this->db->update($this->mytable, $arr_update);
	}

	function insert($arr_insert) {
		$arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
		$this->db->insert($this->mytable, $arr_insert);
		return $this->db->insert_id();
	}

	function delete($val = '', $param = null) {
		if ($val != '') {
			if ($param == null)
				$param = $this->primaryKey;
			if (is_array($val) && $val != array())
				$this->db->where($val);
			else if ($val != '')
				$this->db->where($param, $val);
			$this->db->where($param, $val);
			$this->db->delete($this->mytable);
		}
	}

	
	function key_value($data, $value, $key = '') {
		if (is_array($data)) $data = (object) $data;
		if (!$key) {
			$key = $this->primaryKey;
		}
		$result = array();
		if ($data) foreach ($data as $item) {
			$result[$item->$key] = $item->$value;
		}
		return $result;
	}
}

class CI_Module extends MY_Model {

	var $_layout;
	public static $_widgets = null;
	public static $_modules = null;
	var $_view_dir = 'modules/';
	var $table_modules = "modules";
	var $table_modules_position = "modules_position";
	var $primaryKey = "MOD_ID";

	public function __construct() {
		parent::__construct();

		$controler = $this->router->fetch_class();
		$class = $this->router->fetch_method();
		$cache = $this->config->item('cachePosition');
		$position = strtolower($controler . "_" . $class);

		$this->init();
	}

	public function init() {
		
	}

	public function set_layout($layout) {
		$this->_layout = $layout;
		$this->loads();
	}

	/**
	 * Load db all module;
	 * @param type $param
	 */
	public function loads() {
		$this->load->model('mmodules');
		$this->load->model('mmodulesposition');

		if (CI_Module::$_widgets == null) {
			$this->db->order_by('MPOS_POSITION asc');
			$this->db->order_by('MPOS_ORDER asc');
			$arr_layout = explode('/', $this->_layout);
			$widgets = $this->mmodulesposition->gets(array('MPOS_LAYOUT' => end($arr_layout))); 
			CI_Module::$_widgets = array();
			$module_id = array();
			foreach ($widgets as $widget) {
				if (!isset(CI_Module::$_widgets[$widget->MPOS_POSITION])) {
					CI_Module::$_widgets[$widget->MPOS_POSITION] = array();
				}
				CI_Module::$_widgets[$widget->MPOS_POSITION][] = $widget;
				$module_id[$widget->MOD_ID] = $widget->MOD_ID;
			}
			$module_id = array_keys($module_id);
			if (CI_Module::$_modules == null) {
				if ($module_id) {
					$modules = $this->db
							->where_in('MOD_ID', $module_id)
							->get($this->table_modules)
							->result();
					if ($modules)
						foreach ($modules as $module) {
							CI_Module::$_modules[$module->MOD_ID] = $module;
						}
				}
			}
		}
	}

	public function _active() {
		
		//do something here;
		$this->active();
	}

	public function _deactive() {
		
		//do something here;
		$this->deactive();
	}
	public function active() {
		
	}

	public function deactive() {
		
	}

	public function admin($config) {
		
	}

	public function run($config) {
		
	}
	
	public function module_update($data) {
		return $data;
	}
	

	public function view($view_name, $data, $return = false) {
		return $this->load->view($this->_view_dir . get_class($this) . '/' . $view_name, $data, $return);
	}

	public function is_active_module($name) {
		$module = $this->mmodules->get($name, 'MOD_NAME');
		if ($module)
		return true;
		return false;
	}
}
